# -*- coding: utf-8 -*-
from uiautomator import Device
from multiprocessing import Process, Value,Array
import time
import re
import os
import threading
import pylab as pl
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction


if __name__=="__main__":


    desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['deviceName'] = 'Android Emulator'
    desired_caps['appPackage'] = 'com.example.calculator'
    desired_caps['appActivity'] = '.CalculatorActivity'
	#设置指定ID的手机
	desired_caps['udid']='4d00b50cebd6400d'
    '''
    #设置输入法，结束后恢复输入法，设置指定设备
    desired_caps['unicodeKeyboard']= 'true'
    desired_caps['resetKeyboard']='true'
    desired_caps['udid']='4d00b50cebd6400d'
    '''
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
	
	#启动其他应用，跨APP
	driver.startActivity("com.android.camera", ".CameraLauncher")
	
	button = driver.find_element_by_name('9')
	
	#获得一些控件的信息
	#获取出来的结果是u'txt',u'false'这样的。
	button.get_attribute('enabled')  
	button.get_attribute('clickable')
	button.get_attribute('checked')
	button.get_attribute('selected')
	button.get_attribute('text')
	#控件坐标
	print button.location
    time.sleep(3)

    xy = button.location
    print xy

    XPATH = driver.find_element_by_xpath('//android.widget.Button[@resource-id=\"com.example.calculator:id/num9\"]')
    print XPATH.location
    
	#APP中H5元素的点击
	#返回会话中的上下文
	print driver.contexts
	#将appium设置成WEBVIEW模式，这样就能够识别APP中的H5元素 
	driver.switch_to.context("WEBVIEW")
	time.sleep(5)
	#通过PC端：Chrome 32 +浏览器输入地址chrome://inspect
	driver.find_element_by_id('c_guide').click()

    

    
'''
    driver.press_keycode('26')
    time.sleep(1)
    a=driver.page_source
    print a

    time.sleep(2)
    driver.press_keycode('26')
    time.sleep(2)
    a=driver.page_source
    print a

    driver.swipe(570,1160,570,330)

    time.sleep(2)
    ta = TouchAction(driver)
    ta.press(x=237, y=878).wait(100).move_to(x=300, y=0).wait(200).move_to(x=300, y=0).wait(200).move_to(x=-300, y=300).wait(100).perform()
    time.sleep(1)
    driver.tap([(50,50)])
    time.sleep(3)

    driver.quit()
'''

'''
    print(a)
    print(b)
    if b==u'false' and a==u'÷':
        print('OK')
    else:
        print('NN')

    #做个标记用来停止记录CPU内存数据
    #当数值为1的时候APP_Cpucheck会抛出异常强制停止
    End=[None]

    threads = []
    t1 = threading.Thread(target=APP_Cpucheck)
    threads.append(t1)
    t2 = threading.Thread(target=TTT,args=(200,))
    threads.append(t2)

    for t in threads:
        t.setDaemon(True)
        t.start()
    for t in threads:
        t.join()

    print 'oooookk'

    p = Process(target=worker)
    p2 = Process(target=worker)
    p.start()
    p2.start()
    p.join()
    p2.join()
'''
